#!/bin/bash
# echo the call so we can see what's going on
echo "$0 $1 $2 $3 $4 $5 $6"

# Check syntax
if [ "$1" = "" ]; then
	echo "$0 called without phase number!"
	exit 0
fi

if [ "$2" = "" ]; then
	echo "$0 called without refinement flag!"
	exit 0
fi

if [ "$3" = "" ]; then
	echo "$0 called without damping flag!"
fi

grep NHST `cat GSAS_EXP`.EXP | tail -c67 | head -c65 | sed "s/ //g" > nhist.txt
NHIST=`cat nhist.txt`
echo "We have $NHIST histograms in this refinement."

# vary the phase in all histograms
echo "k l o s" > temp.txt
echo "g" >> temp.txt
echo "1 99" >> temp.txt
echo "$1 $1" >> temp.txt
echo "$2" >> temp.txt
if [ "$3" = "" ]; then
	echo "No damping flag set"
else
	echo "Damping flag is $3, changing all histograms..."
	# set the damping in all histograms
	COUNTER=0
	while [ $COUNTER -lt $NHIST ]; do
		let COUNTER=COUNTER+1 
		echo "d $3" >> temp.txt
		echo "n" >> temp.txt
	done
fi

echo "x" >> temp.txt
echo "x" >> temp.txt
echo "x" >> temp.txt
echo "x" >> temp.txt
echo "x" >> temp.txt

expedt `cat GSAS_EXP` < temp.txt > out.txt

echo "Set refinement flag for weight fraction of phase $1 to $2.\newline" >> temp.tex
